package com.mrd.shiptask_shipowner.hwpush;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.huawei.android.pushagent.api.PushManager;
import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.unit_tools.SysApplication;

public class PustDemoActivity extends Activity {

	public final static String TAG = "PushDemo";

	private static TextView textView;

	public static String[] type = null;

	// 接收Push消息
	public static final int RECEIVE_PUSH_MSG = 0x100;
	// 接收Push Token消息
	public static final int RECEIVE_TOKEN_MSG = 0x101;
	// 接收Push 自定义通知消息内容
	public static final int RECEIVE_NOTIFY_CLICK_MSG = 0x102;
	// 接收Push LBS 标签上报响应
	public static final int RECEIVE_TAG_LBS_MSG = 0x103;

	public static final String NORMAL_MSG_ENABLE = "normal_msg_enable";
	public static final String NOTIFY_MSG_ENABLE = "notify_msg_enable";
	private Button getToken;

	/*
	 * 处理提示消息，更新界面
	 */
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case RECEIVE_PUSH_MSG:
				showMsg((String) msg.obj);
				break;
			case RECEIVE_TOKEN_MSG:
				showMsg((String) msg.obj);
				break;
			case RECEIVE_NOTIFY_CLICK_MSG:
				showMsg((String) msg.obj);
				break;
			case RECEIVE_TAG_LBS_MSG:
				showToast((String) msg.obj);
				break;
			default:
				break;
			}
		}
	};

	public Handler getHandler() {
		return mHandler;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		Log.d(TAG, "run into PustDemoActivity onCreate");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.mian);

		MyApplication.instance().setMainActivity(this);

		textView = (TextView) findViewById(R.id.tv_msg);
		getToken = (Button) findViewById(R.id.getToken);
		getToken.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 点击获取Token
				System.out
						.println("尝试获取token------------------------------------");
				// 获取客户端AccessToken,获取之前请先确定该应用（包名）已经在开发者联盟上创建成功，并申请、审核通过Push权益
				// 该测试应用已经注册过
				PushManager.requestToken(PustDemoActivity.this);

				Log.i(TAG, "try to get Token ,current packageName is "
						+ PustDemoActivity.this.getPackageName());
			}
		});
		SysApplication.getInstance().addActivity(this);
	}

	/*
	 * 显示接收到的Push消息，在页面上
	 */
	public void showMsg(final String msg) {
		try {

			mHandler.post(new Runnable() {

				@SuppressLint("SimpleDateFormat")
				public void run() {
					SimpleDateFormat formatter = new SimpleDateFormat(
							"HH:mm:ss");
					Date curDate = new Date(System.currentTimeMillis());
					String dateStr = formatter.format(curDate);
					textView.postInvalidate();
					String str = "接收时间：" + dateStr + " , 消息内容：" + msg;
					textView.setText(str);
					Log.d(TAG, "showMsg:" + str);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/*
	 * 显示接收到的Push消息，弹出Toast
	 */
	public void showToast(String msg) {
		Log.d(TAG, "showToast:" + msg);
		Toast.makeText(PustDemoActivity.this, msg, Toast.LENGTH_LONG).show();
	}

}
